<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="小哥昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入小哥昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="88px" label="小哥手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入小哥手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="佣金" prop="commissionTotal">
        <el-input
          v-model="queryParams.commissionTotal"
          placeholder="请输入佣金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="commissionRecordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="小哥昵称" align="center" prop="nickName" />
      <el-table-column label="小哥手机号" align="center" prop="phonenumber" />
      <el-table-column label="佣金" align="center" prop="commissionTotal" />
      <!--      <el-table-column-->
      <!--        label="发放日期"-->
      <!--        align="center"-->
      <!--        prop="sendTime"-->
      <!--        width="180"-->
      <!--      >-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.sendTime, "{y}-{m}-{d}") }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleCheck(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改技师佣金发放记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" label-width="80px">
        <el-form-item label="日期">
          <el-date-picker
            clearable
            type="daterange"
            v-model="form.rangeDate"
            value-format="yyyy-MM-dd"
            placeholder="请选择查询日期"
          >
          </el-date-picker> </el-form-item
        ><el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleSearch"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="staticsList">
        <el-table-column label="小哥昵称" align="center" prop="nickName" />
        <el-table-column label="小哥手机号" align="center" prop="phonenumber" />
        <el-table-column label="订单号" align="center" prop="orderId" />
        <el-table-column label="佣金" align="center" prop="commission" />
        <el-table-column label="发放类型" align="center" prop="sendType">
          <template slot-scope="scope">
            <span>{{ scope.row.sendType | formatSendType }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="发放日期"
          align="center"
          prop="sendTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.sendTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { technicianMoneyStatistics } from "@/api/service/commissionRecord";
import { parseTime } from "../../../utils/ruoyi";
import dayjs from "dayjs";
import { listUser } from "@/api/system/user";

export default {
  name: "CommissionRecord",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技师佣金发放记录表格数据
      commissionRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发放日期时间范围
      daterangeSendTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        nickName: undefined,
        phonenumber: undefined,
        orderId: undefined,
        commission: undefined,
        sendTime: undefined,
      },
      // 表单参数
      form: {
        rangeDate: [
          dayjs().startOf("month").format("YYYY-MM-DD"),
          dayjs().endOf("month").format("YYYY-MM-DD"),
        ],
      },
      staticsList: [],
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatSendType(val) {
      if (val) {
        return { 1: "订单完成", 2: "订单打赏" }[val];
      } else {
        return "";
      }
    },
  },
  methods: {
    parseTime,
    /** 查询技师佣金发放记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = { userType: "sys_user" };
      if (null != this.daterangeSendTime && "" != this.daterangeSendTime) {
        this.queryParams.params["beginSendTime"] = this.daterangeSendTime[0];
        this.queryParams.params["endSendTime"] = this.daterangeSendTime[1];
      }
      listUser(this.queryParams).then((response) => {
        this.commissionRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rangeDate: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSendTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSearch() {
      this.getStatisticsList();
    },
    handleCheck(row) {
      this.loading = true;
      this.form.userId = row.userId;
      this.getStatisticsList();
    },
    getStatisticsList() {
      const [beginSendTime, endSendTime] = this.form.rangeDate || [];
      let queryParams = {
        userId: this.form.userId,
        params: {},
      };
      queryParams.params["beginSendTime"] = beginSendTime
        ? `${beginSendTime} 00:00:00`
        : undefined;
      queryParams.params["endSendTime"] = endSendTime
        ? `${endSendTime} 23:59:59`
        : undefined;
      technicianMoneyStatistics(queryParams)
        .then((res) => {
          console.log({ getStatisticsList: res });
          if (res?.code === 200) {
            this.open = true;
            this.staticsList = res?.data?.dataList;
          }
        })
        .finally(() => (this.loading = false));
    },
  },
};
</script>
